#include "key.h"
#include "lcd1602.h"

char MatrixKey(void)
{
	char KeyNumber = No_Press;
	
	P1 = 0xFF;
	P3_7=1;
	P1_4 = 0;
	if(P1_0==0){delay_ms(20);if(P1_0==0){while(P1_0==0);KeyNumber = Number7;}}
	if(P1_1==0){delay_ms(20);if(P1_1==0){while(P1_1==0);KeyNumber = Number4;}}
	if(P1_2==0){delay_ms(20);if(P1_2==0){while(P1_2==0);KeyNumber = Number1;}}
	if(P1_3==0){delay_ms(20);if(P1_3==0){while(P1_3==0);KeyNumber = Rem;}}
	
	P1 = 0xFF;
	P3_7=1;
	P1_5 = 0;
	if(P1_0==0){delay_ms(20);if(P1_0==0){while(P1_0==0);KeyNumber = Number8;}}
	if(P1_1==0){delay_ms(20);if(P1_1==0){while(P1_1==0);KeyNumber = Number5;}}
	if(P1_2==0){delay_ms(20);if(P1_2==0){while(P1_2==0);KeyNumber = Number2;}}
	if(P1_3==0){delay_ms(20);if(P1_3==0){while(P1_3==0);KeyNumber = Number0;}}
	
	P1 = 0xFF;
	P3_7=1;
	P1_6 = 0;
	if(P1_0==0){delay_ms(20);if(P1_0==0){while(P1_0==0);KeyNumber = Number9;}}
	if(P1_1==0){delay_ms(20);if(P1_1==0){while(P1_1==0);KeyNumber = Number6;}}
	if(P1_2==0){delay_ms(20);if(P1_2==0){while(P1_2==0);KeyNumber = Number3;}}
	if(P1_3==0){delay_ms(20);if(P1_3==0){while(P1_3==0);KeyNumber = Point;}}
	
	P1 = 0xFF;
	P3_7=1;
	P1_7 = 0;
	if(P1_0==0){delay_ms(20);if(P1_0==0){while(P1_0==0);KeyNumber = Add;}}
	if(P1_1==0){delay_ms(20);if(P1_1==0){while(P1_1==0);KeyNumber = Sub;}}
	if(P1_2==0){delay_ms(20);if(P1_2==0){while(P1_2==0);KeyNumber = Mul;}}
	if(P1_3==0){delay_ms(20);if(P1_3==0){while(P1_3==0);KeyNumber = Div;}}
	
	P1 = 0xFF;
	P3_7=0;
	if(P1_0==0){delay_ms(20);if(P1_0==0){while(P1_0==0);KeyNumber = BackSpace;}}
	if(P1_1==0){delay_ms(20);if(P1_1==0){while(P1_1==0);KeyNumber = SigClear;}}
	if(P1_2==0){delay_ms(20);if(P1_2==0){while(P1_2==0);KeyNumber = Clear;}}
	if(P1_3==0){delay_ms(20);if(P1_3==0){while(P1_3==0);KeyNumber = Sum;}}
	
	return KeyNumber;
}